package im

import (
	"time"
	//"regexp"
	"dhfshop/models/im"
	"dhfshop/models/isv"
	"dhfshop/controllers/base"
	"github.com/astaxie/beego/orm"
	"github.com/astaxie/beego/validation"
)

type AutoReplyCfgController struct {
	base.BaseController
}

func (this *AutoReplyCfgController) Post() {
    v := im.AutoRspCfg{}
	this.ParseForm(&v)
	v.PartnerId = int64(this.Ctx.Input.GetData("curCompanyId").(int))
	valid := validation.Validation{}
	valid.Required(v.PartnerId, "partner_id")
	valid.Required(v.AppId, "app_id")
	//valid.Required(v.ServiceTypeId, "service_type_id")
	/*
	if v.ServiceTypeId == 2 {
	   valid.Match(v.SessionReplyType, regexp.MustCompile(`^(text|image|link|miniprogrampage)$`), "match")
	   valid.Match(v.RcvmsgReplyType, regexp.MustCompile(`^(text|image|link|miniprogrampage)$`), "match")
	} else {
	   valid.Match(v.SessionReplyType, regexp.MustCompile(`^(text|image|voice|video|music|news|mpnews|wxcard|miniprogrampage)$`), "match")
	   valid.Match(v.RcvmsgReplyType, regexp.MustCompile(`^(text|image|voice|video|music|news|mpnews|wxcard|miniprogrampage)$`), "match")
	   valid.Match(v.SubscribeReplyType, regexp.MustCompile(`^(text|image|voice|video|music|news|mpnews|wxcard|miniprogrampage)$`), "match")
	}
	*/
	passed, _ := valid.Valid(&v)
	if !passed {
	    this.ServeError(valid.Errors[0].Key+" "+valid.Errors[0].Message)
		return
	}
	if err := im.InitDefaultAutoRspCfg(v.PartnerId, v.ServiceTypeId, v.AppId); err != nil{
		this.ServeError(err.Error())
		return
	}
	rsp := struct {
		Id       int64         `json:"partner_id"`
	    Modified time.Time   `json:"modified"`
	}{Id:v.PartnerId, Modified:time.Now().UTC()}
	this.Data["json"] = rsp
	this.ServeJSON()
}	


// 参数查询
func (this *AutoReplyCfgController) Get() {
	//curRoleId := this.Ctx.Input.GetData("curRoleId").(int) 
	companyId := int64(this.Ctx.Input.GetData("curCompanyId").(int))
	curUserId := int64(this.Ctx.Input.GetData("curUserId").(int))
	if curUserId == 0 {
		this.ServeError("user is not login")
	    return
	}
	appId := this.GetString("app_id")
	if appId == "" {
	    this.ServeError("app_id is null")
	    return
	}
	var cfg *im.AutoRspCfg = nil
	var err error
	cfg, err = im.GetAutoRspCfgByAppId(appId)
	if err == orm.ErrNoRows {
	    serviceTypeId := 2
		if custAccount, err := isv.GetCustAccountByAppId(appId); err == nil {
			serviceTypeId = custAccount.ServiceTypeId
		}
		im.InitDefaultAutoRspCfg(companyId, serviceTypeId, appId)   //默认创建为小程序类型
		cfg, err = im.GetAutoRspCfgByAppId(appId)
		if err != nil {
			this.ServeError(err.Error())
			return
		}
	}
	this.Data["json"] = cfg
	this.ServeJSON()
}

// 参数更新
func (this *AutoReplyCfgController) Put() {
    v := im.AutoRspCfg{}
	this.ParseForm(&v)
	v.PartnerId = int64(this.Ctx.Input.GetData("curCompanyId").(int))
	valid := validation.Validation{}
	valid.Required(v.PartnerId, "partner_id")
	valid.Required(v.AppId, "app_id")
	//valid.Required(v.ServiceTypeId, "service_type_id")
	/*
	if v.ServiceTypeId == 2 {
	   valid.Match(v.SessionReplyType, regexp.MustCompile(`^(text|image|link|miniprogrampage)$`), "match")
	   valid.Match(v.RcvmsgReplyType, regexp.MustCompile(`^(text|image|link|miniprogrampage)$`), "match")
	} else {
	   valid.Match(v.SessionReplyType, regexp.MustCompile(`^(text|image|voice|video|music|news|mpnews|wxcard|miniprogrampage)$`), "match")
	   valid.Match(v.RcvmsgReplyType, regexp.MustCompile(`^(text|image|voice|video|music|news|mpnews|wxcard|miniprogrampage)$`), "match")
	   valid.Match(v.SubscribeReplyType, regexp.MustCompile(`^(text|image|voice|video|music|news|mpnews|wxcard|miniprogrampage)$`), "match")
	}
	*/
	passed, _ := valid.Valid(&v)
	if !passed {
	    this.ServeError(valid.Errors[0].Key+" "+valid.Errors[0].Message)
		return
	}
	excludeArr := []string{"partner_id"}
	updateFields, err := this.GetUpdateFields(v, "", "form", excludeArr)
	if err != nil {
	    this.ServeError(err.Error())
		return
	}
	if err = im.UpdateAutoRspCfgById(&v, updateFields...); err != nil{
		this.ServeError(err.Error())
		return
	}
	rsp := struct {
		Id       int64         `json:"partner_id"`
	    Modified time.Time   `json:"modified"`
	}{Id:v.PartnerId, Modified:time.Now().UTC()}
	this.Data["json"] = rsp
	this.ServeJSON()
}	

